import type { TableColumn } from '@/components/core/dynamic-table';
import { type TreeDataItem } from '@/core/permission/utils';

export type TableListItem = API.FlowSortListResultItem;
export type TableColumnItem = TableColumn<TableListItem>;

export const baseColumns: TableColumnItem[] = [
  {
    title: '类型编号',
    dataIndex: 'sortId',
    align: 'center',
    hideInSearch: true,
    width: 160,
  },
  {
    title: '类型名称',
    dataIndex: 'sortName',
    align: 'center',
    hideInSearch: true,
    width: 160,
  },
];

export const formatFlowSortTree = (
  flowsort: API.FlowSortListResultItem[],
  parentId: number | string = 0,
  keyPath: (string | number)[] = [],
): TreeDataItem[] => {
  return flowsort
    .filter((item) => item.sortParentId === parentId)
    .map((item) => {
      const _keyPath = keyPath.concat(parentId || []);
      const arr = formatFlowSortTree(flowsort, item.sortId, _keyPath);
      return Object.assign(item, {
        keyPath: _keyPath,
        title: item.sortName,
        key: item.sortId,
        value: item.sortId,
        formData: item,
        children: arr.length ? arr : null,
      });
    });
};
